angular.module('myApp', ['infinite-scroll']);

function searchOrder($scope, $http) {
	var searchUrl = getUrl("53searchorder");
	var searchHeaders = getHeader();
	var tex = document.getElementById("tex");
	var searchText;
	$scope.Search = function() {
			searchText = tex.value.replace(/\s/g, '');
			if (searchText.length <= 0) {
				mui.toast('请输入姓名、商品名或手机号');
				return;
			}
			plus.nativeUI.showWaiting("正在查找")
			$scope.pages = 0;
			//默认加载第一页，初始值0
			$scope.orderList = [];
			$scope.totalpage = 1;
			$http({
				method: 'POST',
				url: searchUrl,
				headers: searchHeaders,
				timeout: 10000,
				data: 'indexpage=0&pageSize=5' + '&account=' + readData(localStorage.phone) + '&keywords=' + searchText
			}).success(function(data, status, headers, config) {
				plus.nativeUI.closeWaiting()
				if (data.obj.orderList.length > 0) {
					var produ = data.obj.orderList;
					for (var i = 0; i < produ.length; i++) {
						for (var j = 0; j < produ[i].list.length; j++) {
							if (produ[i].list[j].speccolor) {
								produ[i].list[j].ngcolor = "false";
							} else {
								produ[i].list[j].ngcolor = "true";
							}
							if (produ[i].list[j].specsize) {
								produ[i].list[j].ngsize = "false";
							} else {
								produ[i].list[j].ngsize = "true";
							}
							if (produ[i].list[j].specspec) {
								produ[i].list[j].ngspec = "false";
							} else {
								produ[i].list[j].ngspec = "true";
							}
							if (produ[i].list[j].specmodel) {
								produ[i].list[j].ngmodel = "false";
							} else {
								produ[i].list[j].ngmodel = "true";
							}
						}
						if (produ[i].closedComment == "y") {
							produ[i].btn = false;
						} else {
							produ[i].btn = true;
						}
						produ[i].heji = (parseFloat(produ[i].amount) - parseFloat(produ[i].discountPay)).toFixed(2);
					}
					var orderList = produ;
					for (var i = 0; i < orderList.length; i++) {
						orderList[i]['cstatus'] = orderStatus(orderList[i]['status'], orderList[i]['isRefund']);
						//我加的 1订单评价 2付款 3确认收货 5删除订单
						orderList[i]['btnstatus'] = orderStatusCode(orderList[i]['status']);
						if (orderList[i]['isRefund'] == 'n') {
							orderList[i].isShow = true;
						} else {
							orderList[i].isShow = false;
						}
						$scope.orderList.push(orderList[i])
					}
					$scope.totalpage = data.totalpage;
					//下拉页面+1
					$scope.pages = $scope.pages + 1;
				} else {
					mui.toast('无此订单');
				}
			}).error(function(data, status, headers, config) {
				plus.nativeUI.closeWaiting();
				mui.toast("网络连接异常，请稍后重试(e)")
			});
		}
		//加载
	$scope.loadMore = function() {
			if ($scope.pages < $scope.totalpage) {
				params = 'indexpage=' + $scope.pages + '&pageSize=5&account=' + readData(localStorage.phone) + '&keywords=' + searchText;
				$http({
					method: 'POST',
					url: url,
					headers: header,
					timeout: 10000,
					//cache: true,
					data: params
				}).success(function(data, status, headers, config) {
					if (data.obj.orderList.length > 0) {
						var orderList = data.obj.orderList;
						for (var i = 0; i < orderList.length; i++) {
							orderList[i]['cstatus'] = orderStatus(orderList[i]['status']);
							//我加的 1订单评价 2付款 3确认收货 5删除订单
							orderList[i]['btnstatus'] = orderStatusCode(orderList[i]['status']);
							$scope.orderList.push(orderList[i])
						}
						$scope.totalpage = data.totalpage;
						//下拉页面+1
						$scope.pages = $scope.pages + 1;
					};
					//模拟状态数据
				}).error(function(data, status, headers, config) {
					mui.toast('网络链接异常，请稍后重试(e)');
				});
			}
		}
		//申请退货
	$scope.tuihuo = function(order) {
			localStorage.order = writeData(JSON.stringify(order));
			api.openWin({
				name: 'tuihuo',
				url: 'tuihuo.html',
				reload: true
			});
			api.closeWin();
		}
		//订单详情
	$scope.orderDetail = function(order) {
			localStorage.order = writeData(JSON.stringify(order));
			openOrderDetail();
		}
		//评价订单
	$scope.pj = function(order) {
			localStorage.order = writeData(JSON.stringify(order));
			openOrderJudge();
		}
		//支付订单
	$scope.pay = function(order) {
			localStorage.order = writeData(JSON.stringify(order));
			openOrderPay();
		}
		//确认收货
	$scope.check = function(order) {
			var orderId = order.orderId;
			urlc = getUrl("29receiveCheck");
			headerc = getHeader();
			mui.confirm('请确认收到商品后再提交', '确认收货', ['取消', '确认'], function(ret) {
				if (ret.index == 1) {
					plus.nativeUI.showWaiting("请稍等…")
					$http({
						method: 'POST',
						url: urlc,
						headers: headerc,
						timeout: 10000,
						//cache: true,
						data: 'id=' + orderId
					}).success(function(data, status, headers, config) {
						if (data.msg == 'success') {
							refreshOrderManager();
							mui.back();
						} else if (data.code == "1") {
							mui.toast("您的订单还未发货");
						} else {
							mui.toast('网络异常，请稍后重试' + data.code)
						}
					}).error(function(data, status, headers, config) {
						mui.toast('网络异常，请稍后重试')
					});
				}
			});
		}
		//删除订单
	$scope.del = function(order) {
			var orderId = order.orderId;
			urlc = getUrl("30orderDelete");
			headerc = getHeader();
			mui.confirm('订单删除后将无法查看，您确定要删除？', '删除订单', ['取消', '确认'], function(ret) {
				if (ret.index == 1) {
					plus.nativeUI.showWaiting("请稍等…")
					$http({
						method: 'POST',
						url: urlc,
						headers: headerc,
						timeout: 10000,
						//cache: true,
						data: 'id=' + orderId
					}).success(function(data, status, headers, config) {
						if (data.msg == 'success') {
							refreshOrderManager();
							mui.back()
						} else {
							mui.toast('网络异常，请稍后重试' + data.code)
						};
						plus.nativeUI.closeWaiting();
					}).error(function(data, status, headers, config) {
						plus.nativeUI.closeWaiting();
						mui.toast('网络异常，请稍后重试')
					});
				}
			});
		}
		//取消订单
	$scope.cancle = function(order) {
		var orderId = order.orderId;
		urlc = getUrl("45orderCancle");
		headerc = getHeader();
		mui.confirm('确定要取消订单？', '取消订单', ['取消', '确认'], function(ret) {
			if (ret.index == 1) {
				plus.nativeUI.showWaiting("请稍等…");
				$http({
					method: 'POST',
					url: urlc,
					headers: headerc,
					timeout: 10000,
					//cache: true,
					data: 'id=' + orderId
				}).success(function(data, status, headers, config) {
					plus.nativeUI.closeWaiting()
					if (data.msg == 'success') {
						refreshOrderManager();
						mui.back();
					} else {
						mui.toast('网络异常，请稍后重试' + data.code)
					};
				}).error(function(data, status, headers, config) {
					plus.nativeUI.closeWaiting()
					mui.toast('网络异常，请稍后重试')
				});
			}
		});
	}
}

function orderStatusCode(status) {
	var statusCode;
	switch (status) {
		case 'init':
			//未支付
			statusCode = 2;
			break;
		case 'pass':
			//支付  statusCode原来等于3 后来改为了0,0其实无意义
			statusCode = 0;
			break;
		case 'send':
			//发货
			statusCode = 3;
			break;
		case 'sign':
			//收货
			statusCode = 0;
			break;
		case 'cancel':
			//取消
			statusCode = 5;
			break;
		case 'file':
			//已完成
			statusCode = 1;
			break;
		case 'finish':
			//交易关闭
			statusCode = 5;
			break;
	}
	return statusCode
}